// import option from "./echarts.js"

import echarts from "./echarts"

/**
 * echarts    图表 配置库
 * data  后台返回数据
 * type  图表类型
 * callback  回调
 */

export function inflate(echarts, data, type, callback) {
  // if(type==1){

  // echart 所需数据

  const legend = data[0]
  const arr = data.slice(1, data.length) // 数据组数
  // console.log('arr', arr)

  let option

  if (type == 1) {
    option = echarts.lineType
  }

  option.xAxis[0].name = legend[0]

  option.yAxis[0].name = legend[1]

  const xArr = []
  const serY = []

  for (let i = 0; i < arr.length; i++) {
    xArr.push(arr[i][0])
  }
  for (let n = 0; n < arr.length; n++) {
    serY.push(arr[n][1])
  }

  option.xAxis[0].data = xArr

  //    option.yAxis[0].name=yArr

  option.series[0].data = serY

  option.series[0].name = legend[1]

  //    table所需数据

  const tableList = []

  for (let m = 0; m < arr.length; m++) {
    const myObj = new Object()

    for(let k=0;k<legend.length;k++){
      myObj[legend[k]] = arr[m][k]
    }

    // myObj[legend[0]] = arr[m][0]
    // myObj[legend[1]] = arr[m][1]
    // myObj[legend[2]] = arr[m][2]
    // myObj[legend[3]] = arr[m][3]

    tableList.push(myObj)
  }
  callback({ option, tableList, legend })
}

// }

export function inflateZhu(echarts, data, type, callback) {
  const legend = data[0]
  const arr = data.slice(1, data.length) // 数据组数
  // console.log('arr', arr)

  const option = echarts.zhu

  // echars 所需数据

  option.yAxis.data = legend

  const xArr = []
  const serY = []

  for (let i = 0; i < arr.length; i++) {
    xArr.push(arr[i][0])
  }

  //  console.log("L11", arr.length -1)

  for (let n = 0; n < arr.length; n++) {

    
    serY.push(arr[n][data[0].length-1])
  }






  // option.xAxis.name = "123"

  // option.yAxis.name = "456"

  option.yAxis.data = xArr

  option.series[0].data = serY

  const tableList = []

  for (let m = 0; m < arr.length; m++) {
    const myObj = new Object()

    // myObj[legend[0]] = arr[m][0]
    // myObj[legend[1]] = arr[m][1]

    for(let k=0;k<legend.length;k++){
      myObj[legend[k]] = arr[m][k]
    }

    tableList.push(myObj)
  }

  callback({ option, tableList, legend })
}

export function inflateZhu2(echarts, data, type, callback) {
  const legend = data[0].slice(1)
  // console.log(legend)
  const arr = data.slice(1, data.length) // 数据组数

  // echart 所需数据

  echarts.option.yAxis.data = legend

  const serArr = []

  for (let i = 1; i < data.length; i++) {
    // console.log('datai', data[i][0])
    const name1 = data[i][0]
    const data1 = data[i].slice(1)
    const obj = {

      name: name1,
      type: 'bar',
      stack: 'total',

      emphasis: {
        focus: 'series'
      },
      data: data1
    }
    serArr.push(obj)
  }

  echarts.option.series = serArr

  // 表格所需的数据

  const db = JSON.parse(JSON.stringify(arr))

  const tempArr = []
  for (let m = 0; m < db.length; m++) {
    const obj = {}
    for (let n = 0; n < db[0].length; n++) {
      obj[data[0][n]] = db[m][n]
    }

    tempArr.push(obj)
  }
  // console.log('ss', tempArr)

  callback({
    option: echarts.option,
    tableList: tempArr,
    legend: data[0]

  })
}



export function inflatePie(echarts, data, type, callback){




//   0: ["申请人", "专利数量"]
// 1: ["中国科学院大连化学物理研究所", 2]
// 2: ["深圳市冠旭电子股份有限公司", 2]
// 3: ["陈天龙", 2]
// 4: ["东风汽车零部件(集团)有限公司", 1]
// 5: ["友达光电股份有限公司", 1]
// 6: ["常州星宇车灯股份有限公司", 1]
// 7: ["旺矽科技股份有限公司", 1]
// 8: ["李书贤", 1]
// 9: ["杭州亿奥光电有限公司", 1]


  //  echarts 所需数据

  let mydata = data.slice(1)
  // console.log(mydata)

  let tempArr = []
  let legendChart=[]
  mydata.forEach(item=>{
    let obj={
      value:item[1],
      name:item[0]
    }
    legendChart.push(item[0])
    tempArr.push(obj)
  })


  echarts.pie.series[0].data=tempArr



  // table 所需数据

  let legend = data[0]

  

  let table = []
  for (let m = 0; m < mydata.length; m++) {
    const obj = {}
    for (let n = 0; n < legend.length; n++) {
      obj[legend[n]] = mydata[m][n]
    }

    table.push(obj)
  }

  echarts.pie.legend={
    orient: 'vertical',
    type: 'scroll',
    data:legendChart,

    bottom:10,
    right:"10px",
    top:20
  }
  

  callback({
    option: echarts.pie,



    legend:legend,
    tableList:table
  })
}




